<hr class="layui-bg-gray">
<hr class="layui-bg-gray">

<div style="width: 100%;">

    <table style="width: 100%;" id="sea-tab">
        <tr style="width: 100%;height: 40px">
            <td style="text-align: left">> 菜单管理</td>
            <td style="width: 80px; text-align:center"><button onclick="fillMenuInfo()" style="width: 70px; cursor:pointer">+ 新增</button></td>
        </tr>
    </table>

</div>

<hr class="layui-bg-gray" id="menuTree">
<hr class="layui-bg-gray" id="winLayId">

<div id="loadMenuTreeData" class="demo-tree demo-tree-box"></div>


<script>
    layui.use('tree', function() {
        var tree = layui.tree
            , layer = layui.layer

        // 加载菜单及按钮数据
        $.ajax({
            url: '/menu/queryMenuAndChild',
            type: 'POST',
            success: function (res) {
                if (res.errorCode == '0') {
                    // res.data
                    tree.render({
                        elem: '#loadMenuTreeData',
                        data: res.data,
                        customOperate: true,
                        edit: ['update', 'del'], //操作节点的图标
                        operate: function(obj){
                            var type = obj.type; //得到操作类型：add、edit、del
                            var menuId = obj.data.id; //得到当前节点的数据ID

                            // if(type === 'add'){
                            //     fillMenuInfo();
                            // }else
                            if (type == 'del'){
                                deleteMenuByIds(menuId);
                            }
                            if (type == 'update'){
                                loadMenuInfo(menuId);
                            }

                        }
                    });
                }else {
                    layer.msg(res.desc);
                }
            }
        });


    });

</script>

<script>
    $(function () {
        // 加载菜单树数据
        $.ajax({
            url: '/menu/queryMenuAndChild',
            type: 'POST',
            data:{'type':'0'},
            success: function (result) {
                if (result.errorCode == 0) {
                    // result.data
                    $('#menuTree').data('menuTree',result.data);
                }
            }
        });
    })


    // 新增菜单-填写菜单信息
    function fillMenuInfo() {
        layer.open({
            title:'添加菜单',
            id: 'LAY_menuAdd',//设定一个id，防止重复弹出
            type: 1,
            area: ['700px', '450px'],
            shadeClose: true,
            closeBtn: 0,
            btn: ['添加','取消'],
            content: getHtml('/sys/menu/edit.html'),
            yes: function(){
                layer.closeAll();
                // 执行确定逻辑
                addMenu();
            }
            ,btn2: function(){
                layer.closeAll();
            }
        });
    }

    // 执行新增菜单
    function addMenu() {
        let parentId = $('#emPid1').val();
        let name = $('#emName1').val();
        let menuType = $('#emType1').val();
        if (menuType ==="" || undefined || null) { menuType = 0;}

        let pmMark = $('#emMark1').val();
        let visible = $('#emVisible1').val();
        if (visible ==="" || undefined || null) { visible = 1;}

        let icon = $('#emIcon1').val();
        let blank = $('#emBlank1').val();
        if (blank ==="" || undefined || null) { blank = 0;}

        let link = $('#emLink1').val();
        let orderNum = $('#emSort1').val();
        if (orderNum ==="" || undefined || null) {orderNum = 1;}

        let remark = $('#emRemark1').val();

        let url = 'menu/addRecord';
        let params = {'parentId':parentId,'name':name,'menuType':menuType,'pmMark':pmMark,'visible':visible,
            'icon':icon,'blank':blank,'url':link,'orderNum':orderNum,'remark':remark};

        $.ajax({  "type":"post",
            "url":url,
            "data":params,
            "success":function(result){
                //alert(result.desc);
                layer.msg(result.desc);
                loadMenuPaging();
        }})
    }


    // 修改菜单-加载菜单信息
    function loadMenuInfo(id) {
        // 加载菜单及按钮数据
        $.ajax({
            url: '/menu/queryOneById',
            type: 'POST',
            data:{'id':id},
            success: function (result) {
                if (result.errorCode == 0) {
                    // result.data
                    $('#page-com').data('menuData',result.data);

                    layer.open({
                        title:'修改菜单',
                        id: 'LAY_menuUpdate',//设定一个id，防止重复弹出
                        type: 1,
                        area: ['700px', '450px'],
                        shadeClose: true,
                        closeBtn: 0,
                        btn: ['修改','取消'],
                        content: getHtml('/sys/menu/edit.html'),
                        yes: function(){
                            layer.closeAll();
                            // 执行确定逻辑
                            updateMenu();
                        }
                        ,btn2: function(){
                            layer.closeAll();
                        }
                    });
                }else {
                    layer.msg('菜单信息加载失败...');
                }
            }
        });

    };

    // 更新菜单信息
    function updateMenu() {
        let id = $('#emId1').val();
        let parentId = $('#emPid1').val();
        let name = $('#emName1').val();
        let menuType = $('#emType1').val();
        if (menuType ==="" || undefined || null) { menuType = 0;}

        let pmMark = $('#emMark1').val();
        let visible = $('#emVisible1').val();
        if (visible ==="" || undefined || null) { visible = 1;}

        let icon = $('#emIcon1').val();
        let blank = $('#emBlank1').val();
        if (blank ==="" || undefined || null) { blank = 0;}

        let link = $('#emLink1').val();
        let orderNum = $('#emSort1').val();
        if (orderNum ==="" || undefined || null) {orderNum = 1;}

        let remark = $('#emRemark1').val();

        let url = 'menu/updateOneById';
        let params = {'id':id,'parentId':parentId,'name':name,'menuType':menuType,'pmMark':pmMark,'visible':visible,
            'icon':icon,'blank':blank,'url':link,'orderNum':orderNum,'remark':remark};

        $.ajax({  "type":"post",
            "url":url,
            "data":params,
            "success":function(result){
                //alert(result.desc);
                layer.msg(result.desc);
                loadMenuPaging();
        }})
    }

    function deleteMenuByIds(ids) {
        layer.confirm('确定删除吗?', function(){
            let url = 'menu/deleteByIds';
            $.ajax({  "type":"post",
                "url":url,
                "data":{'ids':ids},
                "success":function(result){
                    //alert(result.desc);
                    layer.msg(result.desc);
                    loadMenuPaging();
                }
            })
        });
    }

</script>

